<?php

namespace App\Winged\Patients\Controllers;

use App\Mail\SendMail;
use App\Winged\Patients\Api\Kase;
use Encore\Admin\Facades\Admin;
use App\Winged\Components\Content;
use Illuminate\Http\Request;
use App\Winged\Patients\Api\Patients;
use Illuminate\Support\Facades\Mail;


class PatientsController extends BaseController
{

    public function index()
    {
        return $this->Content(function (Content $content) {
            // filter栏的数据
            $filter = Patients::getPatientStatistic();
            // table表格的数据
            $patients = Patients::makePatients($currentPage = $this->defaultPage, $currentPageSize = $this->defaultPageSize);
            // 总数
            $count = Patients::getPatientCount();
            $view  = view('winged.patients::index', compact('filter', 'patients', 'count', 'currentPage', 'currentPageSize'));
            $content->header('患者列表');
            $content->body($view);
        });
    }

    /**
     * @method POST AJAX
     * @param $condition 12- || 65+ || depart || female || male
     * @param $page 页码　，从０开始
     * @return json
     */
    public function getPatients(Request $request)
    {
        $condition   = $request->input('condition', null);
        $currentPage = $request->input('page', $this->defaultPage);
        if ('0' === $condition) {
            $condition = null;
        }
        if (is_null($condition)) {
            $data = Patients::makePatients($currentPage, $this->defaultPageSize);
        } else {
            $data = Patients::makePatientsByCondition($condition, $currentPage, $this->defaultPageSize);
        }
        return response()->json(compact('data', 'currentPage'));
    }

    /**
     * 患者详情　
     * @return 患者的table
     * @return 患者的详情
     */
    public function show(Request $request)
    {
        $case_id = $request->input('case_id',null);
        $patient_id = $request->input('patient_id',null);
        $currentPage = $request->input('page', $this->defaultPage);
        $mine        = $request->input('mine', 0);
        $patients    = Kase::makeList($patient_id, $currentPage, $currentPageSize = $this->defaultPageSize, $mine);
        if (request()->isMethod('POST')) {
            return response()->json($patients);
        }
        $info = Patients::getPatientDetail($case_id);
        return $this->Content(function (Content $content) use ($patients, $info, $currentPage, $currentPageSize) {
            $package = array_merge($patients, compact('info', 'currentPage', 'currentPageSize'));
            $view    = view('winged.patients::patient.show', $package);
            //$content->header($info['patientName']);
            //$desc    = [];
            //$desc [] = ($info['patientGender'] === 'MALE' ? '男' : '女');
            //$desc [] = $info ['patientAge'] . '岁';
            //$content->description(implode('&nbsp;&nbsp;&nbsp;&nbsp;', $desc));
            $content->body($view);
        });
    }

    // 获取患者信息
    public function getPatientInfo($id)
    {
        $info = Patients::getPatientDetail($id);
        return response()->json($info);
    }


    //判断患者是否存在，存在返回患者详情，不存在返回空数组
    public function existed($id)
    {
        $info = Patients::existed($id);
        return response()->json($info);
    }


    //更新患者信息
    public function update(Request $request)
    {
        $patient_id = $request->input('patient_id');
        $new_patient_id = $request->input('new_patient_id');
        $name = $request->input('name');
        $gender = $request->input('gender');
        $birthday = $request->input('birthday');
        $res = Patients::updatePatientInfo(compact('patient_id','new_patient_id','name','gender','birthday'));
        return response()->json($res);
    }

}
